package com.motorola.dp.jenkinsmonitor.service.process;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.motorola.dp.jenkinsmonitor.dao.SvnModuleDao;
import com.motorola.dp.jenkinsmonitor.model.entity.Customer;

import com.motorola.dp.jenkinsmonitor.model.util.svn.SvnModule;
import com.motorola.dp.jenkinsmonitor.service.entity.CustomerService;

@Service("structureMonitoringService")
public class DefaultStructureMonitoringService implements StructureMonitoringService {

    @Value("#{customerService}")
    private CustomerService customerService;

    @Value("#{svnModuleDao}")
    private SvnModuleDao svnModuleDao;

    @Scheduled(cron = "${customerModules.cron}")
    public void updateCustomerModules() {
        // TODO: update module list for all customers
        List<SvnModule> modules = customerService.fetchCustomerModulesFromSvn("shaw");
    }

    @Scheduled(cron = "${customerList.cron}")
    public void updateCustomerList() {
        // TODO implement this: automatically update existing customers list
        List<Customer> customers = customerService.fetchCustomerList();
        // TODO consider using Hibernate NaturalId to avoid creating duplicate customers in DB
    }

    @Scheduled(cron = "${customerJobs.cron}")
    public void updateCustomerJobs() {
        // TODO implement this: automatically update Jenkins builds for each customer
        // 1. use http://94.246.93.138/view/Code/api/xml to get the list of code-related jobs
        // 2. use http://94.246.93.138/job/[job_name]/api/xml to get job description
        // 3. check description to see if there are any <downstreamProject> dependent elements
        // 4. if there are, repeat 2-4 for each downstream project
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setSvnModuleDao(SvnModuleDao svnModuleDao) {
        this.svnModuleDao = svnModuleDao;
    }
}
